package model.client;

import com.alibaba.fastjson.JSONObject;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import model.bean.Message;
import view.OnLineView;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ClientSendMsg implements Runnable {

    private ObjectOutputStream out;
    private String account;
    private Client client;
    private Socket socket;
    private OnLineView onLineView = null;

    private boolean flag = false;

    public ClientSendMsg(Socket socket, ObjectOutputStream out, String account, Client client, OnLineView onLineView) {
        this.socket = socket;
        this.out = out;
        this.account = account;
        this.client = client;
        this.onLineView = onLineView;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        try {
            JSONObject object = new JSONObject();
            object.put("type", "1");
            object.put("from", account);
            out.writeObject(object);
            out.flush();

            System.out.println(onLineView);

            ((Button)onLineView.findViewById("fSend")).setOnAction(event -> {
                TextArea textArea = (TextArea) onLineView.findViewById("fTextArea");
                String msg = textArea.getText();
                String friend_nickname = ((Label)onLineView.findViewById("friendNickname")).getText();
                System.out.println(msg);
                if (!msg.isEmpty()) {
                    textArea.setText("");
                    String friend_id = "";
                    try {
                        ResultSet resultSet = onLineView.getDatabase().execResult("select user_id from user where nickname = ?", friend_nickname);
                        if (resultSet.next()) {
                            friend_id = resultSet.getString("user_id");
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    try {
                        onLineView.sendMsgToFriend(onLineView.getYourNickname(), msg, friend_nickname);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    JSONObject obj = new JSONObject();
                    //type为2表示这是一条私聊消息，需要服务器转发到指定用户
                    obj.put("type", "2");
                    obj.put("from", String.valueOf(onLineView.getId()));
                    obj.put("to", friend_id);
                    obj.put("msg", msg);
                    try {
                        out.writeObject(obj);
                        out.flush();
                    } catch (Exception e) {
                        try {
                            socket.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            });

            ((Button)onLineView.findViewById("gSend")).setOnAction(event -> {
                TextArea textArea = (TextArea) onLineView.findViewById("gTextArea");
                String msg = textArea.getText();
                String group_name = ((Label)onLineView.findViewById("groupName")).getText();
                if (!msg.isEmpty()) {
                    textArea.setText("");
                    List<String> friends_id = new ArrayList<>();
                    try {
                        String sql = "select group_id from `group` where group_name = ?";
                        ResultSet resultSet = onLineView.getDatabase().execResult(sql, group_name);
                        String group_id = null;
                        if (resultSet.next()) {
                            group_id = resultSet.getString("group_id");
                        }
                        resultSet.close();
                        String sql2 = "select user_id from group_members where group_id = ?";
                        ResultSet resultSet1 = onLineView.getDatabase().execResult(sql2, group_id);
                        while (resultSet1.next()) {
                            friends_id.add(resultSet1.getString("user_id"));
                        }
                        try {
                            System.out.println("ClientSendMsg----" + group_name);
                            onLineView.sendMsgToGroup(onLineView.getYourNickname(), msg, group_name);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        JSONObject obj = new JSONObject();
                        //type为2表示这是一条群聊消息，需要服务器转发到群里所有在线成员处
                        obj.put("type", "3");
                        obj.put("from", String.valueOf(onLineView.getId()));
                        obj.put("gn", group_name);
                        obj.put("to", friends_id);
                        obj.put("msg", msg);

                        try {
                            out.writeObject(obj);
                            out.flush();
                        } catch (Exception e) {
                            try {
                                socket.close();
                            } catch (IOException ex) {
                                ex.printStackTrace();
                            }
                        }

                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
